#include "MyString.h"
#include <iostream>
using namespace std;
#include <cstring>
#include <cstdlib>
//无参构造
MyString::MyString()
{
	this->m_data = new char[1]();
	this->m_data[0] = '\0';
}
//带参构造
MyString::MyString(const char* _str)
{
	if(0 == _str)
	{
		this->m_data = new char[1]();
		this->m_data[0] = '\0';
	}
	else
	{
		int len = strlen(_str);
		this->m_data = new char[len + 1]();
		strcpy(this->m_data, _str);
		this->m_data[len] = '\0';
	}
}
//析构
MyString::~MyString()
{
	if(this->m_data != 0)
	{
		delete [] this->m_data;
	}
}
//拷贝构造
MyString::MyString(const MyString& rhs)
{
	int len = strlen(rhs.m_data);
	this->m_data = new char[len + 1]();
	strcpy(this->m_data, rhs.m_data);
	this->m_data[len] = '\0';
}
//len
int MyString::getLen()const
{
	return strlen(this->m_data);
}
//+
MyString MyString::operator+(const MyString a)const
{
	MyString str1;
	int len=this->getLen() + a.getLen();
	str1.m_data = new char[len+1];
	strcpy(str1.m_data,this->m_data);
	strcat(str1.m_data,a.m_data);
	return str1;
}
//=
int MyString::operator = (const MyString a)
{
	this->m_data = new char[a.getLen()+1];
	strcpy(this->m_data,a.m_data);
	if(strcmp(this->m_data,m_data)==0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
//+=
int MyString::operator += (const MyString a)
{
	MyString str1 = *this + a;
	*this = str1;
	if(strcmp(this->m_data,str1.m_data)==0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
//[]
char& MyString::operator[](int n) const
{
	if (n >= this->getLen())
		return this->m_data[this->getLen() - 1];
	else
		return this->m_data[n];
};
//==
int MyString::operator ==(const MyString a)const
{
	if(strcmp(this->m_data,a.m_data)==0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
	
}
//>
int MyString::operator >(const MyString a)const
{
	if(strcmp(this->m_data,a.m_data)>0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
//<
int MyString::operator <(const MyString a)const
{
	if(strcmp(this->m_data,a.m_data)<0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
//>=
int MyString::operator >=(const MyString a)const
{
	if((this->m_data>a.m_data) || (this->m_data==a.m_data))
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
//<=
int MyString::operator <=(const MyString a)const
{
	if((this->m_data<a.m_data) || (this->m_data==a.m_data))
	{
		return 1;
	}
	else
	{
		return 0;
	}
}